﻿using System;
using System.Collections.Generic;
using TuxedoLib;

namespace SpaceSquirrel
{
	public class Tile
	{
		public int col;
		public int row;
		public bool isDoor;
		public bool isLadder;
		public bool isTop;
		public bool isLava;
		public bool isWater;
		public bool isIce;
		public bool solid;
		public Door door;
		public bool isSpike;
		public bool isBalloon;
		public int[] primaryBalloonOffset;

		public TileTemplate[] templates;
		public TileTemplate[] originalTemplates;
		public int[][] collisions;

		public Tile(IList<TileTemplate> templates, int col, int row)
		{
			this.col = col;
			this.row = row;
			List<TileTemplate> newTemplates = new List<TileTemplate>();
			this.originalTemplates = new List<TileTemplate>(templates).ToArray(); // ugh.
			this.isDoor = false;
			this.isLadder = false;
			this.isTop = false;
			this.isLava = false;
			this.isWater = false;
			this.isIce = false;
			this.door = null;
			this.isSpike = false;
			this.isBalloon = false;
			this.primaryBalloonOffset = null;

			foreach (TileTemplate tile in templates)
			{
				if (tile != null)
				{
					newTemplates.Add(tile);
					if (tile.isDoor) this.isDoor = true;
					if (tile.isLadder) this.isLadder = true;
					if (tile.isTop) this.isTop = true;
					if (tile.isWater) this.isWater = true;
					if (tile.isSpike) this.isSpike = true;
					if (tile.isLava) this.isLava = true;
					if (tile.isIce) this.isIce = true;
					if (tile.isBalloon)
					{
						this.isBalloon = true;
						this.primaryBalloonOffset = tile.primaryBalloonOffset;
					}
				}
			}

			this.templates = new List<TileTemplate>(templates).ToArray();
			List<int[]> collisions = new List<int[]>();
			this.solid = false;

			bool coveredA = false;
			bool coveredB = false;
			bool coveredC = false;
			bool coveredD = false;

			foreach (TileTemplate t in this.templates)
			{
				if (t.solid)
				{
					coveredA = true;
					coveredB = true;
					coveredC = true;
					coveredD = true;
					this.solid = true;
				}
				else
				{
					coveredA = coveredA || t.coveredA;
					coveredB = coveredB || t.coveredB;
					coveredC = coveredC || t.coveredC;
					coveredD = coveredD || t.coveredD;
				}
			}

			bool leftCovered = coveredA && coveredC;
			bool rightCovered = coveredB && coveredD;
			bool topCovered = coveredA && coveredB;
			bool bottomCovered = coveredC && coveredD;

			if (leftCovered && rightCovered)
			{
				collisions.Add(new int[] { 0, 0, 2, 2 });
				coveredA = false;
				coveredB = false;
				coveredC = false;
				coveredD = false;
				this.solid = true;
			}
			else if (leftCovered)
			{
				collisions.Add(new int[] { 0, 0, 1, 2 });
				coveredA = false;
				coveredC = false;
			}
			else if (rightCovered)
			{
				collisions.Add(new int[] { 1, 0, 1, 2 });
				coveredB = false;
				coveredD = false;
			}
			else if (topCovered)
			{
				collisions.Add(new int[] { 0, 0, 2, 1 });
				coveredA = false;
				coveredB = false;
			}
			else if (bottomCovered)
			{
				collisions.Add(new int[] { 0, 1, 2, 1 });
				coveredC = false;
				coveredD = false;
			}

			if (coveredA) collisions.Add(new int[] { 0, 0, 1, 1 });
			if (coveredB) collisions.Add(new int[] { 1, 0, 1, 1 });
			if (coveredC) collisions.Add(new int[] { 0, 1, 1, 1 });
			if (coveredD) collisions.Add(new int[] { 1, 1, 1, 1 });

			this.collisions = collisions.ToArray();

			for (int i = 0; i < this.collisions.Length; ++i)
			{
				int[] c = this.collisions[i];
				c[0] = c[0] * 8 + col * 16;
				c[1] = c[1] * 8 + row * 16;
				c[2] = c[0] + c[2] * 8;
				c[3] = c[1] + c[3] * 8;
			}

			if (this.solid)
			{
				this.isTop = false;
				this.isLadder = false;
			}
		}

		public static Tile Make(TileTemplate lower, TileTemplate upper, int col, int row)
		{
			List<TileTemplate> templates = new List<TileTemplate>();
			if (lower != null) templates.Add(lower);
			if (upper != null) templates.Add(upper);
			return new Tile(templates, col, row);
		}
	}
}
